<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %><%--
用户投票页面
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1 style="color: #3880c2">
    <a href="index.jsp" style="color: crimson">返回主页面</a>
</h1>
<h1 style="color: #3880c2">
    <a href="userPage.jsp" style="color: crimson">返回投票页面</a>
</h1>

<%
    request.setCharacterEncoding("UTF-8");
    int voteId = 0;
    if (request.getParameter("id") != null) {
        try {
            //得到voteId,存入session
            voteId = Integer.parseInt(request.getParameter("id"));
            session.setAttribute("voteId", voteId);
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
            PreparedStatement preparedStatement = connection.prepareStatement("select * from vote_option where vote_id=? ");
            preparedStatement.setInt(1, voteId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {
                out.print("暂无数据!");
            } else {
                out.write("<form action=\"serve/voting.jsp\" method=\"post\">");
                try {
                    //查询全部也可用,先do再while循环
                    //展示所有已经创建投票，只展示标题，附带跳转页面效果，自带id
                    do {
                        int id = resultSet.getInt(1);
                        String option = resultSet.getString(3);
                        //先不展示票的数量
                        //int number = resultSet.getInt(4);
                        out.write(option);
                        out.write("<input type='radio' name='id' value='" + id + "'>" + "<br>");

                    } while (resultSet.next());
                    out.write("<input type=\"submit\" value=\"投票\">");
                    out.print("</form>");
                } catch (Exception e) {
                    out.print(e.getMessage());
                    e.printStackTrace();
                }
            }
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            out.print(e.getMessage());
            e.printStackTrace();
        }
    }
%>
</body>
</html>
